package com.ly.wxstore.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ly.wxstore.comm.Error;
import com.ly.wxstore.entity.Image;
import com.ly.wxstore.entity.PrizeUserinfo;
import com.ly.wxstore.entity.UserInfo;
import com.ly.wxstore.exception.ServerException;
import com.ly.wxstore.service.ImageService;
import com.ly.wxstore.service.PrizeUserinfoService;
import com.ly.wxstore.service.UserInfoService;
import com.ly.wxstore.service.WeixinUserInfoService;
import com.ly.wxstore.service.account.AccountService;

/**
 * 
 * @author Peter
 *
 */
@Controller
@RequestMapping("/admin")
public class IndexController {
	
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat2, true));
	}

	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private AccountService accountService;
	@Autowired
	private ImageService imageService;
	@Autowired
	private WeixinUserInfoService weixinUserInfoService;

	@Autowired
	private PrizeUserinfoService prizeUserinfoService;
	
	
	@RequestMapping(value = "search_1")
	public String apply(Model model) {
		List<UserInfo> list = userInfoService.getAll();
		
		//参与总人次：（就是点击进去的次数）
		Long totalCount = accountService.searchCount();
		//参与总人数：（报名+投票总人数）
		Long totalForCanyu = userInfoService.totalForCanyu();
		//领奖人数：（填写领奖信息的用户）
		Long totalForReceivePrize = userInfoService.totalForReceivePrize();
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("totalForCanyu", totalForCanyu);
		model.addAttribute("totalForReceivePrize", totalForReceivePrize);
		
		//报名人数
		model.addAttribute("applyCount", list.size());
		model.addAttribute("list", list);
		
		
		return "applyList";
	}
	
	@RequestMapping(value = "search_2")
	public String vote(Model model) {
		List<PrizeUserinfo> list = prizeUserinfoService.getAllForVote();
		
		//参与总人次：（就是点击进去的次数）
		Long totalCount = accountService.searchCount();
		//参与总人数：（报名+投票总人数）
		Long totalForCanyu = userInfoService.totalForCanyu();
		//领奖人数：（填写领奖信息的用户）
		Long totalForReceivePrize = userInfoService.totalForReceivePrize();
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("totalForCanyu", totalForCanyu);
		model.addAttribute("totalForReceivePrize", totalForReceivePrize);
		
		//投票人数
		model.addAttribute("voteCount", list.size());
		model.addAttribute("list", list);
		
		
		return "voteList";
	}
	
	@RequestMapping(value = "fancybox")
	public String fancybox(Model model) {
		
		return "fancybox";
	}
	

	@RequestMapping(value="images/{id}",method = RequestMethod.GET)
	public String list(@PathVariable("id") String uid,Model model) {
		List<Image> list = imageService.getByUid(uid);
		model.addAttribute("list", list);
		
		return "imageList";
	}
	
	@RequestMapping(value="userDetail/{id}.json",method = RequestMethod.GET)
	@ResponseBody
	public List<Image> list(@PathVariable("id") String uid) {
		try {
			List<Image> list = imageService.getByUid(uid);
			return list;
		} catch (ServerException e) {
			logger.error(e.getMessage(),e);
			throw e;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new ServerException(Error._10001, Error._10001_MSG);
		}
	}
	
}
